﻿Imports ComponentFactory.Krypton.Toolkit
Imports System.Text.RegularExpressions
Imports System.Data.OleDb

Public Class Assistente

    Public Shared COR_ERRO As Color = Color.FromArgb(255, 206, 125)
    Public Shared COR_NORMAL As Color = Color.White

    Public Shared Function contemRegistro(ByVal sql As String) As Boolean
        Dim con = FabricaDeConexoes.CriarConexao()
        Dim r As Boolean = False
        Try
            con.Open()
            Dim cmd As New OleDbCommand(sql, con)
            r = cmd.ExecuteReader().HasRows
        Catch ex As Exception
            Debug.WriteLine(ex)
        End Try

        Return r
    End Function

    Public Shared Function formataData(ByVal d As Date)
        Return d.ToString("dd/MM/yyyy")
    End Function

    Public Shared Function validaNumero(ByVal num As String) As Boolean
        Dim padrao As String = "^[0-9]+$"
        Dim reg As New Regex(padrao)
        Return reg.Match(num).Success
    End Function

    Public Shared Sub limparCampos(ByVal form As KryptonForm)
        For Each item As Control In form.Controls

            If (item.GetType() Is GetType(KryptonTextBox)) Then
                DirectCast(item, KryptonTextBox).Clear()

            ElseIf (item.GetType() Is GetType(KryptonMaskedTextBox)) Then
                DirectCast(item, KryptonMaskedTextBox).Clear()

            ElseIf (item.GetType() Is GetType(KryptonListBox)) Then
                DirectCast(item, KryptonListBox).Items.Clear()

                'Método Que Limpa as ComboBox
                'ElseIf (item.GetType() Is GetType(KryptonComboBox)) Then
                'DirectCast(item, KryptonComboBox).SelectedItem = Nothing
                'DirectCast(item, KryptonComboBox).Text = Nothing 
            End If

        Next
    End Sub

    Public Shared Function verificarExistencia(ByVal valor As Object, ByVal lista As Object()) As Boolean

        For i = 0 To lista.Length - 1
            If (valor = lista(i)) Then
                Return (True)
            ElseIf (i = lista.Length - 1) Then
                Return (False)
            End If
        Next

    End Function

    '''--------------------------------------------------
    '''<summary>
    ''' Função que remove pontos de uma String.
    ''' Recomendado utilizar em campos com máscara.
    ''' Ex.: CPF, CNPJ, Inscrição Estadual.
    ''' </summary>
    '''
    '''<param name="str">
    ''' O valor que contém a máscara.
    ''' Ex.: CPF -> "111.222.333-96"
    ''' </param>
    '''
    ''' <returns>Uma String sem máscara. Ex.: 11122233396</returns>
    '''--------------------------------------------------
    ''' 

    Public Shared Function removePontos(ByVal str As String) As String
        Dim s As String = Nothing

        s = String.Concat(str.Split(New Char() {".", ",", "/", "\", "-", "(", ")"}))

        Return (s)
    End Function

    Public Shared Sub trocaCorCampos(ByVal c() As Control, ByVal cor As Color)
        For Each i In c
            If (i.GetType() Is GetType(KryptonTextBox)) Then
                DirectCast(i, KryptonTextBox).StateCommon.Back.Color1 = cor

            ElseIf (i.GetType() Is GetType(KryptonMaskedTextBox)) Then
                DirectCast(i, KryptonMaskedTextBox).StateCommon.Back.Color1 = cor

            ElseIf (i.GetType() Is GetType(KryptonListBox)) Then
                DirectCast(i, KryptonListBox).StateCommon.Back.Color1 = cor

            ElseIf (i.GetType() Is GetType(KryptonComboBox)) Then
                DirectCast(i, KryptonComboBox).StateCommon.ComboBox.Back.Color1 = cor
            End If
        Next
    End Sub

    Public Shared Function validarEspacoEmBranco(ByVal str As String) As Boolean
        For Each i As Char In str
            'Se houver espacos em branco, retorne falso
            If (Char.IsWhiteSpace(i)) Then
                Return False
            End If
        Next
        Return True
    End Function

    Public Shared Sub trocaCorTransition(ByVal form As KryptonForm)
        For Each item As Control In form.Controls

            If (item.GetType() Is GetType(KryptonTextBox)) Then
                DirectCast(item, KryptonTextBox).StateCommon.Back.Color1 = Color.White

            ElseIf (item.GetType() Is GetType(KryptonMaskedTextBox)) Then
                DirectCast(item, KryptonMaskedTextBox).StateCommon.Back.Color1 = Color.White
            End If
        Next

    End Sub

End Class